﻿using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.SignalR.Client;
using System.ComponentModel.DataAnnotations;
using TrackSystem.IO.Formula;
using TrackSystem.IO.Sensors;

namespace TrackSystem.UI.Server.Units
{
    public partial class SensorView
    {
        [Parameter]
        public SensorUnit snr { get; set; }

        [Parameter]
        public HubConnection hub { get; set; }

        [Inject]
        private DialogService? DialogService { get; set; }

        /// <summary>
        /// 修改值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        private async Task SetValue(string name, bool flag)
        {
            try
            {
                if (hub != null && hub.State == HubConnectionState.Connected)
                    await hub.SendAsync("Assign", $"{name}_Enable", new OperandDef(flag));
            }
            catch (Exception _)
            {
            }
        }

        private string ByteArrayToHexString(byte[] data)
        {
            if (data == null || data.Length == 0) return string.Empty;
            return BitConverter.ToString(data).Replace("-", " ");
        }

        /// <summary>
        /// 修改变量值
        /// </summary>
        /// <param name="regName"></param>
        /// <param name="regValue"></param>
        /// <returns></returns>
        private async Task ChangValueInt(string regName, double regValue)
        {
            var data = new FooInt { val = (int)regValue };

            var option = new EditDialogOption<FooInt>()
            {
                Title = regName + " 数据修改",
                Model = data,
                RowType = RowType.Inline,
                ShowLoading = true,
                ItemsPerRow = 1,
                ItemChangedType = ItemChangedType.Update,
                OnEditAsync = async context =>
                {
                    await hub.SendAsync("Assign", regName, new OperandDef(data.val));
                    return true;
                }
            };
            await DialogService.ShowEditDialog(option);
        }

        /// <summary>
        /// 修改变量值
        /// </summary>
        /// <param name="regName"></param>
        /// <param name="regValue"></param>
        /// <returns></returns>
        private async Task ChangValueDouble(string regName, double regValue)
        {
            var data = new FooDouble { val = regValue };

            var option = new EditDialogOption<FooDouble>()
            {
                Title = regName,
                Model = data,
                RowType = RowType.Inline,
                ShowLoading = true,
                ItemsPerRow = 1,
                ItemChangedType = ItemChangedType.Update,
                OnEditAsync = async context =>
                {
                    await hub.SendAsync("Assign", regName, new OperandDef(data.val));
                    return true;
                }
            };
            await DialogService.ShowEditDialog(option);
        }
    }
}
